package com.pickme.food.task;

import com.pickme.food.mapper.OrdersMapper;
import com.pickme.food.pojo.Stats;
import com.pickme.food.service.inte.StatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 定时统计每日的经营数据
 */

@Slf4j
@Component
public class StatsTask {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private StatsService statsService;

    /**
     * 定时任务每天 00:00:00 执行一次，统计前一天经营数据
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void statsDayDate() {
        String date = LocalDate.now().minusDays(1).toString();
        Stats stats = ordersMapper.getDayStats(date);
        stats.setStatsDate(date);
        stats.setCreateTime(LocalDateTime.now());
        statsService.save(stats);
        log.info("已统计 " + date + " 经营数据");
    }
}
